/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xyserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xyserver.entity.XyServer;
import vip.xiaonuo.modular.xyserver.enums.XyServerExceptionEnum;
import vip.xiaonuo.modular.xyserver.mapper.XyServerMapper;
import vip.xiaonuo.modular.xyserver.param.XyServerParam;
import vip.xiaonuo.modular.xyserver.service.XyServerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 服务相关service接口实现类
 *
 * @author yanwei
 * @date 2023-05-10 17:35:38
 */
@Service
public class XyServerServiceImpl extends ServiceImpl<XyServerMapper, XyServer> implements XyServerService {

    @Override
    public PageResult<XyServer> page(XyServerParam xyServerParam) {
        QueryWrapper<XyServer> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyServerParam)) {

            // 根据客服名称 查询
            if (ObjectUtil.isNotEmpty(xyServerParam.getServerName())) {
                queryWrapper.lambda().eq(XyServer::getServerName, xyServerParam.getServerName());
            }
            // 根据富文本 查询
            if (ObjectUtil.isNotEmpty(xyServerParam.getInfos())) {
                queryWrapper.lambda().eq(XyServer::getInfos, xyServerParam.getInfos());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyServer> list(XyServerParam xyServerParam) {
        return this.list();
    }

    @Override
    public void add(XyServerParam xyServerParam) {
        XyServer xyServer = new XyServer();
        BeanUtil.copyProperties(xyServerParam, xyServer);
        this.save(xyServer);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyServerParam> xyServerParamList) {
        xyServerParamList.forEach(xyServerParam -> {
            this.removeById(xyServerParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyServerParam xyServerParam) {
        XyServer xyServer = this.queryXyServer(xyServerParam);
        BeanUtil.copyProperties(xyServerParam, xyServer);
        this.updateById(xyServer);
    }

    @Override
    public XyServer detail(XyServerParam xyServerParam) {
        return this.queryXyServer(xyServerParam);
    }

    /**
     * 获取服务相关
     *
     * @author yanwei
     * @date 2023-05-10 17:35:38
     */
    private XyServer queryXyServer(XyServerParam xyServerParam) {
        XyServer xyServer = this.getById(xyServerParam.getId());
        if (ObjectUtil.isNull(xyServer)) {
            throw new ServiceException(XyServerExceptionEnum.NOT_EXIST);
        }
        return xyServer;
    }

    @Override
    public void export(XyServerParam xyServerParam) {
        List<XyServer> list = this.list(xyServerParam);
        PoiUtil.exportExcelWithStream("SnowyXyServer.xls", XyServer.class, list);
    }

}
